﻿/*
 * Created by SharpDevelop.
 * User: wbettencourt
 * Date: 02/11/2011
 * Time: 01:26 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace Principal
{
	/// <summary>
	/// Description of TipoAtencionDB.
	/// </summary>
	public class TipoAtencionDB
	{
	    private MySqlConnection _conn;
	    
	    private MySqlCommand _insert;
	    private MySqlCommand _select;
	    private MySqlCommand _delete;
	    
		public TipoAtencionDB(MySqlConnection conn)
		{
		    this._conn = conn;
		    
		    this._insert = new MySqlCommand("INSERT INTO TipoAtencion VALUES (null, @Nombre, @Descripcion, null)", this._conn);
		    this._insert.Parameters.Add("@Nombre", MySqlDbType.VarChar);
		    this._insert.Parameters.Add("@Descripcion", MySqlDbType.VarChar);
		    
		    this._select = new MySqlCommand("SELECT IdTipoAtencion, Nombre, Descripcion FROM tipoatencion", this._conn);
		    
		    this._delete = new MySqlCommand("DELETE FROM tipoatencion WHERE IdTipoAtencion = @Id", this._conn);
		    this._delete.Parameters.Add("@Id", MySqlDbType.Int32);
		}
		
		public int Insert(String Nombre, String Descripcion)
		{
		    int resp = 0;
		    try
		    {
		        this._conn.Open();
	            this._insert.Parameters["@Nombre"].Value = Nombre;
	            this._insert.Parameters["@Descripcion"].Value = Descripcion;
	            this._insert.ExecuteNonQuery();
	            //resp = (int)this._insert.ExecuteScalar();
		    }
		    catch (Exception err)
		    {
		        throw new DatabaseOperationException("Error de Conexión con la Base de Datos.", err);
		    }
		    finally
		    {
		        if (this._conn != null)
		        {
		            this._conn.Close();
		        }
		    }
		    return resp;
		}
		
		public bool Delete(int id)
		{
		    try
		    {
		        this._conn.Open();
		        this._delete.Parameters["@Id"].Value = id;
		        this._delete.ExecuteNonQuery();
		    }
		    catch (Exception err)
		    {
		        throw new DatabaseOperationException("Error de Conexión con la Base de Datos.", err);
		    }
		    finally
		    {
		        if (this._conn != null)
		        {
		            this._conn.Close();
		        }
		    }
		    return false;
		}
		
		public bool Update(int id, String Nombre, String Descripcion)
		{
		    return false;
		}
		
		public DataTable Load()
		{
		    try
		    {
		        MySqlDataAdapter adapter = new MySqlDataAdapter(this._select);
		        DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds.Tables[0];
		    }
		    catch (Exception err)
		    {
		        throw new DatabaseOperationException("Error de Conexión con la Base de Datos.", err);
		    }
		}
	}
}
